package utils;

import java.util.*;
import java.io.*;

import android.content.res.Resources;
import app.*;

import com.StarMicronics.StarIOSDK.R;

public class FilePack {
	public static Resources resources;
	
	private static int __current_lan;
	private static Dictionary<Integer, FilePack> __mute_dic;
	
	private Vector<String> texts;
	
	public final static void setCurrentlanguage(int lan) {
		if (__mute_dic == null) __mute_dic = new Hashtable<Integer, FilePack>();
		FilePack pack = __mute_dic.get(Integer.valueOf(lan));
		if (pack == null) {
			pack = new FilePack(lan);
			__mute_dic.put(Integer.valueOf(lan), pack);
		}
		__current_lan = lan;
		AppDelegate.sharedInstance().postNotification(MultiLanguageChangeNotificationListener.class, null, null);
	}
	
	public final static int currentLanguage() {
		return __current_lan;
	}
	
	public final static String currentLanguageString() {
		if (__current_lan == R.raw.c_en_us) {
			return "en-US";
		} else if (__current_lan == R.raw.c_es_mx) {
			return "es-MX";
		} else {
			return null;
		}
	}
	
	public final static String getStringByID(int text) {
		FilePack pack = __mute_dic.get(Integer.valueOf(__current_lan));
		if (pack == null) {
			return null;
		} else {
			if (text >= 0 && text < pack.texts.size()) {
				return (String) pack.texts.elementAt(text);
			} else {
				return null;
			}
		}
	}
		
	private int readInt(DataInputStream in) throws IOException {
		return (in.readByte()&0xFF)|((in.readByte()&0xFF)<<8)|((in.readByte()&0xFF)<<16)|((in.readByte()&0xFF)<<24);
	}
	
	private FilePack(int lan) {
		try {
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			
	        InputStream inStream = resources.openRawResource(lan);
	        int propertiesSize = 5;
			byte[] properties = new byte[propertiesSize];
			if (inStream.read(properties, 0, propertiesSize) != propertiesSize)
				throw new Exception("input .lzma file is too short");
			SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
			if (!decoder.SetDecoderProperties(properties))
				throw new Exception("Incorrect stream properties");
			long outSize = 0;
			for (int i = 0; i < 8; i++)
			{
				int v = inStream.read();
				if (v < 0)
					throw new Exception("Can't read stream size");
				outSize |= ((long)v) << (8 * i);
			}
			if (!decoder.Code(inStream, outStream, outSize))
				throw new Exception("Error in data stream");
			
			outStream.flush();
			byte[] data = outStream.toByteArray();
			outStream.close();
			inStream.close();
			
			DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
			int numOfString = this.readInt(in);
			int[] points = new int[numOfString+1];
			Vector<String> v = new Vector<String>();
			for (int i = 0; i < numOfString+1; i++) {
				points[i] = this.readInt(in);
			}
			for (int i = 0; i < numOfString; i++) {
				int len = points[i+1]-points[i];
				byte[] temp = new byte[len];
				System.arraycopy(data, points[i], temp, 0, len);
				String str = new String(temp, "UTF-8");
				v.add(str);
			}
			texts = v;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
